package com.nageoffer.shortlink.admin.controller;

import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.common.convention.result.Results;
import com.nageoffer.shortlink.admin.dto.req.ShortLinkGroupReqDTO;
import com.nageoffer.shortlink.admin.dto.req.ShortLinkGroupSortReqDTO;
import com.nageoffer.shortlink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import com.nageoffer.shortlink.admin.dto.resp.ShortLinkGroupRespDTO;
import com.nageoffer.shortlink.admin.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1")
public class GroupController {

    private final GroupService groupService;

    /**
     * 新增短链接分组
     *
     * @param shortLinkGroupReqDTO
     * @return
     */
    @PostMapping("/group")
    public Result<Void> save(@RequestBody ShortLinkGroupReqDTO shortLinkGroupReqDTO) {
        groupService.saveGroup(shortLinkGroupReqDTO.getName());
        return Results.success();
    }

    /**
     * 短链接分组查询集合
     *
     * @return
     */
    @GetMapping("/group")
    public Result<List<ShortLinkGroupRespDTO>> listGroup() {
        return Results.success(groupService.listGroup());
    }

    /**
     * 修改短链接分组
     *
     * @param shortLinkGroupUpdateReqDTO
     * @return
     */
    @PutMapping("/group")
    public Result<Void> updateGroup(@RequestBody ShortLinkGroupUpdateReqDTO shortLinkGroupUpdateReqDTO) {
        groupService.updateGroup(shortLinkGroupUpdateReqDTO);
        return Results.success();
    }

    /**
     * 删除短链接分组
     *
     * @param gid
     * @return
     */
    @DeleteMapping("/group")
    public Result<Void> deleteGroup(@RequestParam String gid) {
        groupService.deleteGroup(gid);
        return Results.success();
    }

    /**
     * 短链接分组排序
     *
     * @param shortLinkGroupSortReqDTOList
     * @return
     */
    @PostMapping("/group/sort")
    public Result<Void> sortGroup(@RequestBody List<ShortLinkGroupSortReqDTO> shortLinkGroupSortReqDTOList) {
        groupService.sortGroup(shortLinkGroupSortReqDTOList);
        return Results.success();
    }


}
